package com.blx.utils.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper{

	public XssHttpServletRequestWrapper(HttpServletRequest servletRequest){
		super(servletRequest);
	}
	public String[] getParameterValues(String parameter){
		String values[] = super.getParameterValues(parameter);
		if(null == values){
			return null;
		}
		int count = values.length;
		String encodedValues[] = new String[count];
		for(int i=0;i<count;i++){
			encodedValues[i] = clearXss(values[i]);
		}
		return encodedValues;
	}
	public String getParameter(String parameter){
		String value = super.getParameter(parameter);
		if(null == value){
			return null;
		}else{
			return clearXss(value);
		}
	}
	public String getHeader(String name){
		String value = super.getHeader(name);
		if(null == value){
			return null;
		}else{
			return clearXss(value);
		}
	}
	//将<,>,(,),转义
	private String clearXss(String value) {
		value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
		value = value.replaceAll("\\(", "&#40;").replaceAll("\\)", "&#41;");
		value = value.replaceAll("'", "&#39;");
		return value;
	}
}
